/*
 * Created by JFormDesigner on Sun Jun 23 15:53:29 CST 2024
 */

package scct.chenhui.forms;

import scct.chenhui.dao.UserDao;
import scct.chenhui.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author Administrator
 */
public class AddUserDialog2 extends JDialog {
    int id;
    private User user;

    public AddUserDialog2(Window owner,int id) {
        super(owner);
        initComponents();
        this.id=id;
        initView();
    }

    private void initView() {
        UserDao userDao = new UserDao();
        user = userDao.getUserById(id);
        System.out.println(user.toString());
        txtUserName.setText(user.getUsername());
        txtPass.setText(user.getPassword());
        txtMobile.setText(user.getMobile());
        System.out.println(user.getSex());
         if(user.getSex().equals("男"))
         {
             radMale.setSelected(true);
             System.out.println("male");
         }else{
             radFemale.setSelected(true);
             System.out.println("female");
         }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        String name = txtUserName.getText();
        String pass = txtPass.getText();
        String mobile = txtMobile.getText();
        String sex=radMale.isSelected()?"男":"女";
        if(!name.matches("^\\w{3,8}$"))
        {
            JOptionPane.showMessageDialog(null,"请输入合法的用户名");
            return;
        }
        if(!pass.matches("^\\w{3,8}$"))
        {
            JOptionPane.showMessageDialog(null,"请输入合法的密码名");
            return;
        }
        if(!mobile.matches("^\\d{11}$"))
        {
            JOptionPane.showMessageDialog(null,"请输入有效的手机号码");
            return;
        }
        UserDao userDao = new UserDao();
        if(userDao.updateUser(new User(name,id,pass,user.getRole(),sex,mobile)))
        {
            JOptionPane.showMessageDialog(null,"修改成功");
            dispose();
        }

        
    }

    private void btnCancel(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - hui chen
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        txtUserName = new JTextField();
        label2 = new JLabel();
        txtPass = new JTextField();
        radMale = new JRadioButton();
        label3 = new JLabel();
        radFemale = new JRadioButton();
        label4 = new JLabel();
        txtMobile = new JTextField();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setTitle("\u4fee\u6539\u7528\u6237");
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax . swing
            . border .EmptyBorder ( 0, 0 ,0 , 0) ,  "JFor\u006dDesi\u0067ner \u0045valu\u0061tion" , javax. swing .border . TitledBorder
            . CENTER ,javax . swing. border .TitledBorder . BOTTOM, new java. awt .Font ( "Dia\u006cog", java .
            awt . Font. BOLD ,12 ) ,java . awt. Color .red ) ,dialogPane. getBorder () ) )
            ; dialogPane. addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (java . beans. PropertyChangeEvent e
            ) { if( "bord\u0065r" .equals ( e. getPropertyName () ) )throw new RuntimeException( ) ;} } )
            ;
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- label1 ----
                label1.setText("\u7528\u6237\u540d");
                contentPanel.add(label1);
                label1.setBounds(40, 20, 50, 20);
                contentPanel.add(txtUserName);
                txtUserName.setBounds(100, 10, 185, 35);

                //---- label2 ----
                label2.setText("\u5bc6\u7801");
                contentPanel.add(label2);
                label2.setBounds(40, 70, 45, 25);
                contentPanel.add(txtPass);
                txtPass.setBounds(100, 65, 185, 35);

                //---- radMale ----
                radMale.setText("\u7537");
                radMale.setSelected(true);
                contentPanel.add(radMale);
                radMale.setBounds(100, 160, 50, 25);

                //---- label3 ----
                label3.setText("\u6027\u522b");
                contentPanel.add(label3);
                label3.setBounds(40, 160, 40, 20);

                //---- radFemale ----
                radFemale.setText("\u5973");
                contentPanel.add(radFemale);
                radFemale.setBounds(165, 160, 50, 25);

                //---- label4 ----
                label4.setText("\u7535\u8bdd");
                contentPanel.add(label4);
                label4.setBounds(40, 120, 40, 20);
                contentPanel.add(txtMobile);
                txtMobile.setBounds(100, 115, 185, 35);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("\u4fee\u6539");
                okButton.addActionListener(e -> btnAdd(e));
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText("\u53d6\u6d88");
                cancelButton.addActionListener(e -> btnCancel(e));
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        ButtonGroup buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radMale);
        buttonGroup1.add(radFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - hui chen
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JTextField txtUserName;
    private JLabel label2;
    private JTextField txtPass;
    private JRadioButton radMale;
    private JLabel label3;
    private JRadioButton radFemale;
    private JLabel label4;
    private JTextField txtMobile;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
